const app = getApp()
import util from '../../../utils/util'
import {
  GetVollegeDetail,
  GetGoodsList
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl:app.globalData.baseImgUrl,
    id: '',
    infos: '',
    photos: [],
    videos: [],
    scenery: [],
    facilities: [],
    goods: [],
    toggle_status: false,
    cur_index: 0,
    page: 1,
    limit: 10,
    isfixed: false,
    showVideo: false,
    videoLink: '',
    curVideo:'',
    showVr: false,
    vrItem: '',
    isEmpty: false, // 是否为空
    isBottom: false, // 是否无数据触底了
    isReachBtm: false, // 是否触发上拉加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getDetail();
    this.getGoods();
  },

  getDetail() {
    let that = this;
    util.apiRequest(GetVollegeDetail, {
      id: app.globalData.vollege_id,
     // id: 46,
    }, null).then(r => {
      console.log('乡村详情：', r.data.data);
      that.setData({
        infos: r.data.data.data,
        photos: r.data.data.photos,
        scenery: r.data.data.scenery,
        videos: r.data.data.video,
        facilities: r.data.data.facilities
      })
    });
  },

  getGoods() {
    let that = this;
    util.apiRequest(GetGoodsList, {
      type: 2,
      vollege_id: app.globalData.vollege_id,
      page: that.data.page,
      limit: that.data.limit
    }, null).then(r => {
      console.log('农产品：', r.data.data);
      var newList = that.data.goods.concat(r.data.data)
      if (r.data.data.length == 0) {
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      }
      that.setData({
        goods: newList
      })
      wx.hideLoading();
      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }
    });
  },

  toGoodsDetail: function (e) {
    wx.navigateTo({
      url: '/pages/goods/goods_detail/goods_detail?id=' + e.currentTarget.dataset['id'],
    });
  },

  toggle() {
    this.setData({
      toggle_status: !this.data.toggle_status
    })
  },

  onTab(e) {
    console.log("tab:", e.currentTarget.dataset.index)
    let i = e.currentTarget.dataset.index;
    this.setData({
      cur_index: e.currentTarget.dataset.index
    })
    if(i == 3){
      this.getGoods()
    }
  },

  previewImg: function(e) {
    console.log('预览处理图片e:', e)
    let that = this;
    //var index = e.currentTarget.dataset.index;
    let imgs = e.currentTarget.dataset.imgs

    wx.previewImage({
      //当前显示图片
      current: imgs[0],
      //所有图片
      urls: imgs
    })
  },

  onShowVideo(e){
    console.log('显示视频:',e)
    this.setData({
      showVideo:true,
      curVideo: e.currentTarget.dataset.item
     // videoLink:e.currentTarget.dataset.item.link
    })
  },

  closeVideo(e) {
    let id = e.currentTarget.dataset.id
    let videoContext = wx.createVideoContext('video'+id)
    videoContext.pause()
    this.setData({
      showVideo:false
    })
  },

  checkVR(e) {
    this.setData({
      showVr:true,
      vrItem: e.currentTarget.dataset.item
    })
   // this.vrItem = item;
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var page = this.data.page;
    that.setData({
      isReachBtm: true
    })
    if (that.data.isBottom) return;
    page++;
    that.setData({
      page
    })
    console.log('page++', page)
    this.getGoods()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})